/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.merchant;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.utils.IdGen;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.merchant.ReceiptTime;
import com.neusoft.lohas.modules.ivm.entity.merchant.TimeSlot;
import com.neusoft.lohas.modules.ivm.service.merchant.ReceiptTimeService;
import com.neusoft.lohas.modules.sys.entity.Dict;
import com.neusoft.lohas.modules.sys.utils.DictUtils;

/**
 * 收货时间Controller
 * @author Jackie.Liu
 * @version 2014-10-28
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/product/receipttime")
public class ReceiptTimeController extends BaseController {

	@Autowired
	private ReceiptTimeService receiptTimeService;
	
	@ModelAttribute
	public ReceiptTime get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return receiptTimeService.get(id);
		}else{
			return new ReceiptTime();
		}
	}
	
	@RequiresPermissions("ivm:product:receipttime:view")
	@RequestMapping(value = "form")
	public String form(ReceiptTime receiptTime, Model model) {
		if (StringUtils.isBlank(receiptTime.getId())) {
			receiptTime.setMerchant(super.merchant);
			receiptTime = receiptTimeService.findByMerchantId(receiptTime.getMerchant().getId());
		}
		model.addAttribute("receiptTime", receiptTime);
		return "modules/ivm/set/receiptTimeForm";
	}

	@RequiresPermissions("ivm:product:receipttime:edit")
	@RequestMapping(value = "save")
	public String save(ReceiptTime receiptTime, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, receiptTime)){
			return form(receiptTime, model);
		}
		if (StringUtils.isBlank(receiptTime.getId())) {
			receiptTime.setMerchant(super.merchant);
			receiptTime.setId(IdGen.uuid());
			receiptTime.prePersist();
		}
		if (receiptTime.getTimeSlots().size()>0) {
			for (TimeSlot time : receiptTime.getTimeSlots()) {
				if (StringUtils.isBlank(time.getId())) {
					time.setReceiptTime(receiptTime);
					time.setId(IdGen.uuid());
					time.prePersist();
				}
				//更新时间段与配送地区关系
				List<Dict> areaList = Lists.newArrayList();
				List<String> areaIdList = time.getAreaIdList();
				for (Dict area : DictUtils.getDictList("city_area")){
					if (areaIdList.contains(area.getId())){
						areaList.add(area);
					}
				}
				time.setAreaList(areaList);
			}
		}
		receiptTimeService.save(receiptTime);
		addMessage(redirectAttributes, "保存收货时间成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/receipttime/form";
	}
	
	@RequiresPermissions("ivm:product:receipttime:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		receiptTimeService.delete(id);
		addMessage(redirectAttributes, "删除收货时间成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/product/receipttime/?repage";
	}
	
}
